<?php

class LinkController extends Controller{
    /**
	 * @return array action filters
	 */
	public function filters(){
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
            'postOnly + lock',
		);
	}
    
    /**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules(){
		return array(
            array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('delete'),
				'roles'=>array('admin'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('create','update','index','lock', 'category'),
				'roles'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
            
		);
	}
	public function actionIndex(){
        $criteria=new CDbCriteria();
        $criteria->order = 'create_date DESC';
        $count = Link::model()->count($criteria);
        $pages=new CPagination($count);

        // results per page
        $pages->pageSize=20;
        $pages->applyLimit($criteria);
        $models = Link::model()->findAll($criteria);
        
        $this->render('index', array(
            'models' => $models,
            'pages' => $pages
        ));
	}
    
    public function actionCreate(){
        $model = new Link();
        
        $this->performAjaxValidation($model);
        
        if(isset($_POST['Link'])){
			$model->attributes=$_POST['Link'];
			if($model->save()){
                $this->redirect(array('index'));
            }
        }

        
        $this->render('create',array('model'=>$model));
    }
    
    /**
     * 更新
     * @param type $id
     */
    public function actionUpdate($id){
        $model = $this->loadModel($id);
        
        $this->performAjaxValidation($model);
        
        if(isset($_POST['Link'])){
			$model->attributes=$_POST['Link'];
			if($model->save()){
                $this->redirect(array('index'));
            }
        }
        
        $this->render('create',array('model'=>$model));
    }
    
    public function actionDelete(){
        $id = $_POST['id'];
        $model = $this->loadModel($id);
        $model->is_delete = (int)!$model->is_delete;
        $model->save();
        $message = array();
        $message['code'] = 1;
        $message['status'] = $model->is_delete;
        echo json_encode($message);
    }
    
    public function actionLock(){
        $id = $_POST['id'];
        $model = $this->loadModel($id);
        $model->is_lock = (int)!$model->is_lock;
        $model->save();
        $message = array();
        $message['code'] = 1;
        $message['lock'] = $model->is_lock;
        echo json_encode($message);
    }
    
    public function actionCategory(){
        $criteria = new CDbCriteria();
        $criteria->order = 'id ASC';
        $category = Category::model()->findAll($criteria);
        
        $categoryListCriteria = new CDbCriteria();
        $categoryListCriteria->condition = 'category=:category';
        $categoryListCriteria->params = array(':category'=>'link');
        $categoryListCriteria->order = 'id DESC';
        $count = Type::model()->count();
        $pages = new CPagination($count);
        $pages->pageSize = 20;
        $pages->applyLimit($categoryListCriteria);
        $categoryList = Type::model()->findAll($categoryListCriteria);

        $this->render('category',array(
            'category'=>$category,
            'models'=>$categoryList,
            'pages'=>$pages
        ));

    }

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
    
    /**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model){
		if(isset($_POST['ajax']) && $_POST['ajax']==='link-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
    
    /**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
    public function loadModel($id){
		$model = Link::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'请求的页面不存在.');
		return $model;
	}
}